<?php
namespace app\home\controller;
use think\Db;
use clt\Leftnav;
use think\facade\Request;
use think\Controller;
class Common extends Controller{
    protected $pagesize;
    public function initialize(){
        $sys = F('System');
        $this->assign('sys',$sys);
        $this->assign('domain',request()->domain());
        //获取控制方法
        $request = Request::instance();
        $action = $request->action();
        $controller = $request->controller();
        $this->assign('action',($action));
        $this->assign('controller',strtolower($controller));
        define('MODULE_NAME',strtolower($controller));
        define('ACTION_NAME',strtolower($action));

        //导航
        $thisCat = db('category')->where('id',input('catId'))->find();
        $this->assign('title',$thisCat['title']);
        $this->assign('keywords',$thisCat['keywords']);
        $this->assign('description',$thisCat['description']);
        define('DBNAME',strtolower($thisCat['module']));
        $this->pagesize = $thisCat['pagesize']>0 ? $thisCat['pagesize'] : '';
        // 获取缓存数据
        $cate = cache('cate'); //左侧工具导航
        if(!$cate){
            $column_one = db('category')->where(['parentid'=>0,'ismenu'=>1])->order('listorder')->select();
            $column_two = db('category')->where(['ismenu'=>1])->order('listorder')->select();;
            $tree = new Leftnav ();
            $cate = $tree->index_top($column_one,$column_two);
            cache('cate', $cate, 3600);
        }
        foreach($cate as $k => $v){
            $cate[$k]['article'] = db('article')->where('catid',$v['id'])->order(['order'=>'desc'])->select();
        }
        $stock = $cate[0]; // 常用工具
        $social = $cate[1]; // 社交
        unset($cate[0]);
        unset($cate[1]);

        $this->assign('stock',$stock);
        $this->assign('social',$social);
        $this->assign('category',$cate);
        //站点
        $thisCat = db('categoryama')->where('id',input('catId'))->find();
        $this->assign('title',$thisCat['title']);
        $this->assign('keywords',$thisCat['keywords']);
        $this->assign('description',$thisCat['description']);
        define('DBNAME',strtolower($thisCat['module']));
        $this->pagesize = $thisCat['pagesize']>0 ? $thisCat['pagesize'] : '';
        // 获取缓存数据
        $cateama = cache('cateama');
        if(!$cateama){ //跨境圈导航
            $column_one = db('categoryama')->where(['parentid'=>0,'ismenu'=>1])->order('listorder')->select();
            $column_two = db('categoryama')->where(['ismenu'=>1])->order('listorder')->select();;
            $tree = new Leftnav ();
            $cateama = $tree->index_top($column_one,$column_two);
            cache('cateama', $cateama, 3600);
        }
        
        $this->assign('categoryama',$cateama);
        //广告
        $adList = cache('adList');
        if(!$adList){
            $adList = db('ad')->where(['type_id'=>1,'open'=>1])->order('sort asc')->limit('2')->select();
            cache('adList', $adList, 3600);
        }

        $this->assign('adList', $adList);
        $year = date('Y');
        $this->assign('year', $year);
        //招聘专栏
        $recruit = cache('recruit');
        if(!$recruit){
            $recruit = db('recruit')->select();
            cache('recruit', $recruit, 3600);
        }
        $this->assign('recruit', $recruit);
        //友情链接
        $linkList = cache('linkList');
        if(!$linkList){
            $linkList = db('link')->where('open',1)->order('sort asc')->select();
            cache('linkList', $linkList, 3600);
        }
		$this->assign('linkList', $linkList);

        //服务提供
        $serviceList = cache('serviceList');     
        if(!$serviceList) {
            $serviceList = db('service')->order('listorder','asc')->select();
            cache('serviceList', $serviceList, 3600);
        }

        foreach ($serviceList as $k=>$v) {
            $serviceList[$k]['article'] = db('servicearticle')->where('service_id',$v['id'])->order('listorder','asc')->limit(4)->select();
        }
        $this->assign('serviceList', $serviceList);
    }
    public function _empty(){
        return $this->error('空操作，返回上次访问页面中...');
    }
}